#!/bin/bash

#lancia ucarpp n volte per ogni particolare file all'interno di una particolare cartella

#./executeAlghoritm.bash 1 /home/koldar/Documents/CC++/Algoritmi\ di\ Ottimizzazione/benchmarks/instances/ /home/koldar/Desktop/output
#lancia questo script in Algoritmi di Ottimizzazione/Release!

#numero esecuzioni per file grafo
runPerFile=$1
#directory dove si trovano i grafi
graphDirectory=$2
#output directory
outputDirectory=$3

maxfile=""
max=0
current=0

for file in `ls -l "$graphDirectory" | tr -s " " | cut -d" " -f9`
do
	for vehicle in 2 3 4
	do
		max=0
		current=0
		maxfile=""
		for runID in `seq 1 $runPerFile`
		do
			echo "************* "`basename $file .dat`" K=$vehicle #run=$runID/$runPerFile *****************" 
			echo "" > config.txt
			echo '"percorso grafo": "'$graphDirectory'/'$file'";' >> config.txt
			echo '"numero veicoli": '$vehicle';' >> config.txt
			echo '"nome immagine costruttiva": "costruttiva.bmp";' >> config.txt
			echo '"bmp per LOS {true,false}": false;' >> config.txt
			echo '"octave per LOS {true,false}": true;' >> config.txt
			echo '"metodo di valutazione {0,1,2} (pure/smart/wise)": 1;' >> config.txt
			echo '"iterazioni massime LOS": 1000;' >> config.txt
			echo '"iterazioni peggiorative massime": 50;' >> config.txt
			echo '"iterazioni massime STM": 1;' >> config.txt
			echo '"tenure base ADD": 5;' >> config.txt
			echo '"tenure base REMOVE": 3;' >> config.txt
		
			./ucarpp config.txt > $outputDirectory/`basename $file .dat`"_"$vehicle"_run"$runID
			cp ../matlab-interface/fObjHistory.m $outputDirectory/`basename $file .dat`"_"$vehicle"_run"$runID"_fObjHistory.m"
			current=$(cat $outputDirectory/`basename $file .dat`"_"$vehicle"_run"$runID | tail -n 1 | tr -d "[a-zA-Z. ]")
			echo $current" "$max
			if [[ $current -gt $max ]]
			then
				if [[ $max -ne 0 ]]
				then
					rm $maxfile
					rm $maxfile"_fObjHistory.m"
				fi
				maxfile=$outputDirectory/`basename $file .dat`"_"$vehicle"_run"$runID
				max=$current
			else
				rm $outputDirectory/`basename $file .dat`"_"$vehicle"_run"$runID
				rm $outputDirectory/`basename $file .dat`"_"$vehicle"_run"$runID"_fObjHistory.m"
			fi
		done
	done
	
done
echo 

